REM >Director:Menus.System.Openfiles

REM Show all the open files on the desktop
REM Original by Dave Lawrence
REM Changes by Nick Craig-Wood, inluding ideas by Aidan Corey
REM Modified by Cy Booker so sorts by name
REM   Modifications copyright  Cy Booker 1995
REM   Note this should now handle very long file names
REM   At the expense of requiring 150K
REM   But I've got 18Mb so I dont care!

ON ERROR ERROR EXT ERR,REPORT$+" at line "+STR$ ERL:END

*Menu "" CloseAll
  *Option "Close"
    *Command SWI "XOS_FSControl" 22
*EndMenu

*Set Director$Menu Files
size=256
DIM buffer size
DIM files 256*512
DIM pointers 256*4
SYS "Director_Menu","OpenFiles"+" "+"Files -temp"
os_args=FNswi_number("XOS_Args")
option=FNswi_number("Director_Option")
command=FNswi_number("Director_Command")
num=0
FOR h=255 TO 0 STEP -1
  SYS os_args,254,h TO S%
  IF (S% AND &800)=0 THEN
    SYS os_args,7,h,buffer,,,size TO ,,,,,spare ;V
    IF (V AND 1)=0 THEN
      f = files + (512 * num)
      PROCputs(f, """"+RIGHT$("   "+STR$ h,3)+" ")
      pointers!(num*4) = f
      PROCputm(f, buffer, size-spare)
      PROCputs(f, " (")
      REM Thanks to Aidan Corey for this bit
      IF (S% AND &40) THEN PROCputs(f, "R")
      IF (S% AND &100) THEN PROCputs(f, "W") ELSE IF (S% AND &80) PROCputs(f, "w")
      IF (S% AND &7000) THEN PROCputs(f, "X")
      IF PTR#h THEN PROCputs(f, " &"+STR$~(PTR#h))
      PROCputs(f, ")")
      PROCputs(f, """ -sub Dynamic:/Director:Menus.System.OpenSub")
      num+=1
    ENDIF
  ENDIF
NEXT
IF num<>0 THEN
  SYS "OS_HeapSort", num, pointers, 4
  WHILE num>0
    num -= 1
    SYS option, !pointers - 5
    pointers += 4
  ENDWHILE
ELSE
  *Option "None" -grey
ENDIF
SYS "Director_EndMenu"
END

DEF FNswi_number(swi$)
  SYS "OS_SWINumberFromString",,swi$ TO swi%
=swi%

DEF PROCputs(RETURN f, A$)
  $f = A$
  f += LEN A$
ENDPROC

DEF PROCputm(RETURN f, A%, B%)
LOCAL C%
  C% = f
  f += B%
  WHILE B%>0
    B% -= 1
    C%?B% = A%?B%
  ENDWHILE
ENDPROC

